﻿
using Microsoft.EntityFrameworkCore.Metadata;
using Models.Common;
using Models.Device;
using Models.TaskPlain;
using NetTaste;
using Newtonsoft.Json;
using System;
using UtilsORM.MySqlData;
using static Models.Common.EnumDeviceState;
using UtilsBase;
using GatewayServerService.Device;
using GatewayServerService.Data;
using static Models.Common.EnumDeviceModelType;
using NewLife.Redis.Core;
using UtilsCache;

namespace GatewayServerService.CloudCenter
{
    public class ServiceCloudCallBack
    {

        //如果有485异常设备，会导致占用端口，其它设备延执行
        //回调获获状态,为了避免误差，查状态和执行，都返回了 当前开关状态
        public void callBackUpdate(MQReqModel<object> httpReqModel)
        {
            try
            {
                ModelReceverData modelDeviceData = httpReqModel.toServerData;
                CommandLogModel commandLogModel = httpReqModel.toGatewayData;

                // if (commandLogModel.agreementType == EnumAgreementType.RS485 && commandLogModel.repeatCount != -100 && (modelDeviceData == null || string.IsNullOrWhiteSpace(modelDeviceData.commandResults[0].value)))
                // {
                //     httpReqModel.respCode = ResponseCode.System_ERROR;
                //    httpReqModel.respMsg = "rs485返回值为空";
                // }

                // iot_device_model_type model_Type = new ServiceIotDeviceModelTypeCache().getModelTypeByDeviceCache(commandLogModel.d_guid);
                if (httpReqModel.respCode == ResponseCode.System_ERROR)
                {
                    //更新设备状态，设备状态异常
                    string respStatus = UtilMemoryCacheHelper.get<string>("respCode" + commandLogModel.d_guid);
                    if (string.IsNullOrEmpty(respStatus))
                    {//错误状态更新
                        new ServiceCloudDeviceLiveState().setDeviceLiveState(commandLogModel.d_guid, (int)Device_State.error, httpReqModel.respMsg);
                        UtilMemoryCacheHelper.set("respCode" + commandLogModel.d_guid, "error", 10);
                    }
                    new ServiceCloudDeviceCommand().writeCommandLog(httpReqModel);
                    return;//不再向下执行
                }
                else
                { //更新设备状态，设备状态异常-->变成正常
                    string respStatus = UtilMemoryCacheHelper.get<string>("respCode" + commandLogModel.d_guid);
                    if (!string.IsNullOrEmpty(respStatus) && respStatus == "error")
                    {
                        new ServiceCloudDeviceLiveState().setDeviceLiveState(commandLogModel.d_guid, (int)Device_State.normal, "异常-》恢复正常");
                        UtilMemoryCacheHelper.remove("respCode" + commandLogModel.d_guid);
                    }
                }

                // switch的效率其实与分支数无关。只有当分支比较少时，if语句的效率才比switch高，因为switch有跳转表。当分支较多时，推荐使用switch语句。

                switch (commandLogModel.deviceModelCode)
                {
                    //如果返回内容为空，下面不执行了
                    case "1013":
                        //普锐森社 RS485光照温湿三合一(pr - 3002 - gzws - n01)
                        new ServiceDeviceRS4851013_GWS().callBackUpdate(httpReqModel);
                        break;
                    case "1014":

                        //普锐森社 RS485-PH检测 液体PR-*-PH-N01 温度
                        new ServiceDeviceRS4851014_PH().callBackUpdate(httpReqModel);
                        break;
                    case "1015":

                        //普锐森社 RS485-ec检测
                        new ServiceDeviceRS4851015_EC().callBackUpdate(httpReqModel);
                        break;
                    case "1016":

                        //普锐森社 土壤温湿度传感器水分变送器农业大棚土质肥力氮磷钾酸碱度检测仪
                        new ServiceDeviceRS4851016_TR().callBackUpdate(httpReqModel);
                        break;
                    case "1018":

                        //485继电器485继电器8路
                        new ServiceDeviceRS4851018_JDQ8().callBackUpdate(httpReqModel);
                        break;

                    case "1034":

                        //485继电器16路，中盛
                        new ServiceDeviceRS4851034_JDQ16().callBackUpdate(httpReqModel);

                        break;
                    case "1021":

                        //485继电器2路
                        new ServiceDeviceRS4851021_JDQ2().callBackUpdate(httpReqModel);
                        break;
                    case "1019":

                        //脉冲
                        new ServiceDeviceRS4851019_MC2().callBackUpdate(httpReqModel);
                        break;
                    case "1001":
                        //gpio
                        new ServiceDeviceGpio1001().callBackGpioStatus(httpReqModel);
                        break;

                    case "1004":
                        new ServiceDeviceI2C1004_SHT30WS().callBackUpdate(httpReqModel);
                        break;
                    case "1023":
                        //普锐森社-温湿光照co2传感器
                        new ServiceDeviceRS4851023_GWSCO2().callBackUpdate(httpReqModel);
                        break;

                    case "1024":

                        //中盛-脉冲计数器 8路+470-4分
                        new ServiceDeviceRS4851024().callBackUpdate(httpReqModel);
                        break;
                    case "1026":

                        //中盛-脉冲计数器 8路+138+3.5mm
                        new ServiceDeviceRS4851026_MC8().callBackUpdate(httpReqModel);
                        break;
                    case "1025":

                        //普锐森社-风速变送器风
                        new ServiceDeviceRS4851025_FS().callBackUpdate(httpReqModel);
                        break;
                    case "1030":

                        //普锐森社-雨雪传感器
                        new ServiceDeviceRS4851030_YX().callBackUpdate(httpReqModel);
                        break;
                    case "1031":

                        //普锐森社-大气压传感器
                        new ServiceDeviceRS4851031_QY().callBackUpdate(httpReqModel);
                        break;
                    case "1032":

                        //普锐森社-风向传感器
                        new ServiceDeviceRS4851032_FX().callBackUpdate(httpReqModel);
                        break;
                    case "1033":

                        //普锐森社-室内噪声传感器
                        new ServiceDeviceRS4851033_ZY().callBackUpdate(httpReqModel);
                        break;
                    case "1035":

                        //空调红外遥控器RS584
                        new ServiceDeviceRS4851035_HW().callBackUpdate(httpReqModel);
                        break;
                    case "1036":

                        //普锐森社 RS485-PH检测 液体PR-*-PH-N01
                        new ServiceDeviceRS4851036_PH().callBackUpdate(httpReqModel);
                        break;
                    case "1040":

                        //普锐森社 RS485-PH检测 液体PR-*-PH-N01
                        new ServiceDeviceRS4851040().callBackUpdate(httpReqModel);
                        break;
                    case "1043":

                        //	GPS/北斗定位模块
                        new ServiceDeviceRS4851043_GPS_BEIDOU().callBackUpdate(httpReqModel);
                        break;
                    case "1044":
                        //	EC 10-20000
                        new ServiceDeviceRS4851044_EC10_20000().callBackUpdate(httpReqModel);
                        break;
                    default:
                        new DBIotSystemLogError().addSystemLogError(commandLogModel.d_guid, commandLogModel.device_remark, "callBackUpdate MQ消息处理， 设备：d_guid:" + commandLogModel.d_guid + " 没有该设备型号的处理方法");
                        break;
                }


                //条件处理
                new ServiceCloudLinkageCondition().linkageConditionAutoRun(commandLogModel.d_guid);



                //写日志
                new ServiceCloudDeviceCommand().writeCommandLog(httpReqModel);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}
